package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.base.dto.BaseQuery;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.UacGroupLanguage;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.UacGroupLanguageService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020-01-16 20:13
 * @Description:
 */
@RestController
@RequestMapping(value = "/language", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UacGroupLanguageController extends BaseController {

    @Resource
    private UacGroupLanguageService uacGroupLanguageService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @PostMapping(value = "/save")
    public Wrapper save(@RequestBody UacGroupLanguage uacGroupLanguage) {

        Integer result = uacGroupLanguageService.saveLanguage(uacGroupLanguage);
        if (result < 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        } else {
            redisTemplate.opsForValue().set(RedisKeyUtil.getUacGroupLanguageId(uacGroupLanguage.getGroupId()),
                    uacGroupLanguage.getLanguage());
        }

        //return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null));
    }

    @PostMapping(value = "/deleteByGroupId/{groupId}")
    public Wrapper deleteByGroupId(@PathVariable Long groupId) {

        Integer result = uacGroupLanguageService.deleteByKey(groupId);
        if (result < 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        } else {
            //
            redisTemplate.delete(RedisKeyUtil.getUacGroupLanguageId(groupId));
        }

        //return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null));
    }

    @PostMapping(value = "/queryByGroupId/{groupId}")
    public Wrapper queryByGroupId(@PathVariable Long groupId) {

        UacGroupLanguage uacGroupLanguage = uacGroupLanguageService.selectByKey(groupId);
        if (PublicUtil.isEmpty(uacGroupLanguage)) {
            uacGroupLanguage = new UacGroupLanguage();
            uacGroupLanguage.setGroupId(groupId);
            uacGroupLanguage.setLanguage("zh_CN");
        }

        //return WrapMapper.ok(uacGroupLanguage);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), uacGroupLanguage);
    }

    @PostMapping(value = "/queryListWithPage")
    public Wrapper save(@RequestBody BaseQuery baseQuery) {

        PageHelper.startPage(baseQuery.getPageNum(), baseQuery.getPageSize());

        List<UacGroupLanguage> uacGroupLanguageList = uacGroupLanguageService.selectAll();

        //return WrapMapper.ok(new PageInfo<>(uacGroupLanguageList));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), new PageInfo<>(uacGroupLanguageList));
    }

}
